<?php

/**
 * index.php
 * 
 * @package Main
 */




require_once("lib/user.lib.php");
require_once("lib/session.lib.php");
require_once("lib/table.lib.php");
require_once("lib/game.lib.php");
require_once("lib/player.lib.php");






$func["user_add_new_user"]["username"] = "";
$func["user_add_new_user"]["md5pw"] = "";
$func["user_add_new_user"]["no_escape"] = array("BOOL_FALSE", "BOOL_TRUE");

$func["user_check_password"]["username"] = "";
$func["user_check_password"]["md5pw"] = "";
$func["user_check_password"]["no_escape"] = array("BOOL_FALSE", "BOOL_TRUE");

$func["user_is_exists"]["username"] = "";
$func["user_is_exists"]["no_escape"] = array("BOOL_FALSE", "BOOL_TRUE");

$func["user_get_points"]["user_id"] = "";



$func["session_set"]["user_id"] = "";
$func["session_set"]["session_id"] = "BOOL_FALSE";
$func["session_set"]["no_escape"] = array("BOOL_FALSE", "BOOL_TRUE");


$func["session_update_heartbeat"]["session_id"] = "";
$func["session_update_heartbeat"]["no_escape"] = array("BOOL_FALSE", "BOOL_TRUE");

$func["session_is_valid"]["session_id"] = "";
$func["session_is_valid"]["no_escape"] = array("BOOL_FALSE", "BOOL_TRUE");

$func["session_postpone_expiry"]["session_id"] = "";
$func["session_postpone_expiry"]["postpone_time"] = SITE_DEFAULT_LOGIN_EXPIRE_TIME;
$func["session_postpone_expiry"]["no_escape"] = array("BOOL_FALSE", "BOOL_TRUE");

$func["session_gen_id"]= "";

$func["session_clear_expired"]= "";

$func["session_id_is_exist"]["session_id"] = "";
$func["session_id_is_exist"]["no_escape"] = array("BOOL_FALSE", "BOOL_TRUE");


$func["session_get_user_id"]["session_id"] = "";
$func["session_get_user_id"]["no_escape"] = array("BOOL_FALSE", "BOOL_TRUE");


$func["table_get_user_table"]["user_id"] = "";
$func["table_get_user_table"]["no_escape"] = array("BOOL_FALSE", "BOOL_TRUE");

$func["table_get_user_table_seat"]["table_id"] = "";
$func["table_get_user_table_seat"]["user_id"] = "";
$func["table_get_user_table_seat"]["no_escape"] = array("BOOL_FALSE", "BOOL_TRUE");

$func["table_get_table_seat_user"]["table_id"] = "";
$func["table_get_table_seat_user"]["seat_number"] = "";
$func["table_get_table_seat_user"]["no_escape"] = array("BOOL_FALSE", "BOOL_TRUE");


$func["table_user_stand_up"]["user_id"] = "";
$func["table_user_stand_up"]["no_escape"] = array("BOOL_FALSE", "BOOL_TRUE");


$func["table_sit_down"]["table_id"] = "";
$func["table_sit_down"]["seat_number"] = "";
$func["table_sit_down"]["user_id"] = "";
$func["table_sit_down"]["no_escape"] = array("BOOL_FALSE", "BOOL_TRUE");

$func["table_stand_up"]["table_id"] = "";
$func["table_stand_up"]["seat_number"] = "";
$func["table_stand_up"]["no_escape"] = array("BOOL_FALSE", "BOOL_TRUE");

$func["table_get_users"]["table_id"] = "";
$func["table_get_users"]["no_escape"] = array("BOOL_FALSE", "BOOL_TRUE");

$func["table_remove_game"]["table_id"] = "";

$func["table_get_game_id"]["table_id"] = "";

$func["table_assoc_game"]["table_id"] = "";
$func["table_assoc_game"]["game_id"] = "";




$func["table_initialize"]["num_of_tables"] = GAME_DEFAULT_NUM_OF_TABLE;


$func["game_add_new"]["player_no"] = "";

$func["game_make_new"]["table_id"] = "";
$func["game_make_new"]["no_escape"] = array("BOOL_FALSE", "BOOL_TRUE");

$func["game_set_waterlevel"]["game_id"] = "";
$func["game_set_waterlevel"]["round"] = "";
$func["game_set_waterlevel"]["turn"] = "";
$func["game_set_waterlevel"]["max_waterlevel"] = "";
$func["game_set_waterlevel"]["min_waterlevel"] = "";

$func["game_set_player_hand"]["game_id"] = "";
$func["game_set_player_hand"]["user_id"] = "";
$func["game_set_player_hand"]["round"] = "";
$func["game_set_player_hand"]["card"] = "";

$func["game_set_current_round"]["game_id"] = "";
$func["game_set_current_round"]["current_round"] = "";

$func["game_set_current_turn"]["game_id"] = "";
$func["game_set_current_turn"]["current_turn"] = "";

$func["game_gen_waterlevel"]["game_id"] = "";

$func["game_gen_player_hand"]["game_id"] = "";


$func["game_get_num_of_players"]["game_id"] = "";

$func["game_get_current_round"]["game_id"] = "";

$func["game_get_current_turn"]["game_id"] = "";

$func["game_get_users"]["game_id"] = "";

$func["game_get_status"]["game_id"] = "";

$func["game_set_status"]["game_id"] = "";
$func["game_set_status"]["status"] = "";

$func["game_start_game"]["game_id"] = "";

$func["game_num_of_active_player"]["game_id"] = "";



$func["game_get_turn_max_waterlevel"]["game_id"] = "";
$func["game_get_turn_max_waterlevel"]["round"] = "";
$func["game_get_turn_max_waterlevel"]["turn"] = "";

$func["game_get_turn_min_waterlevel"]["game_id"] = "";
$func["game_get_turn_min_waterlevel"]["round"] = "";
$func["game_get_turn_min_waterlevel"]["turn"] = "";

$func["game_set_player_waterlevel"]["game_id"] = "";
$func["game_set_player_waterlevel"]["user_id"] = "";
$func["game_set_player_waterlevel"]["round"] = "";
$func["game_set_player_waterlevel"]["turn"] = "";
$func["game_set_player_waterlevel"]["waterlevel"] = "";
$func["game_set_player_waterlevel"]["buoy_remain"] = "";

$func["game_end_turn"]["game_id"] = "";

$func["game_end_round"]["game_id"] = "";
$func["game_end_round"]["end_now"] = array("BOOL_FALSE", "BOOL_TRUE");

$func["game_end_game"]["game_id"] = "";


$func["player_play_card"]["game_id"] = "";
$func["player_play_card"]["user_id"] = "";
$func["player_play_card"]["round"] = "";
$func["player_play_card"]["turn"] = "";
$func["player_play_card"]["card"] = "";

$func["player_is_card_valid"]["game_id"] = "";
$func["player_is_card_valid"]["user_id"] = "";
$func["player_is_card_valid"]["round"] = "";
$func["player_is_card_valid"]["turn"] = "";
$func["player_is_card_valid"]["card"] = "";



$func["player_play_card_auto"]["game_id"] = "";
$func["player_play_card_auto"]["user_id"] = "";
$func["player_play_card_auto"]["card"] = "";

$func["player_set_waterlevel"]["game_id"] = "";
$func["player_set_waterlevel"]["user_id"] = "";
$func["player_set_waterlevel"]["round"] = "";
$func["player_set_waterlevel"]["waterlevel"] = "";

$func["player_set_turn"]["game_id"] = "";
$func["player_set_turn"]["round"] = "";
$func["player_set_turn"]["turn"] = "";

$func["player_set_buoy_remain"]["game_id"] = "";
$func["player_set_buoy_remain"]["user_id"] = "";
$func["player_set_buoy_remain"]["round"] = "";
$func["player_set_buoy_remain"]["buoy_remain"] = "";


$func["player_get_current_waterlevel"]["game_id"] = "";
$func["player_get_current_waterlevel"]["user_id"] = "";
$func["player_get_current_waterlevel"]["round"] = "";

$func["player_get_buoy_remain"]["game_id"] = "";
$func["player_get_buoy_remain"]["user_id"] = "";
$func["player_get_buoy_remain"]["round"] = "";

$func["player_get_hand"]["game_id"] = "";
$func["player_get_hand"]["user_id"] = "";
$func["player_get_hand"]["round"] = "";

$func["player_get_hand_remain"]["game_id"] = "";
$func["player_get_hand_remain"]["user_id"] = "";
$func["player_get_hand_remain"]["round"] = "";

$func["player_get_hand_played"]["game_id"] = "";
$func["player_get_hand_played"]["user_id"] = "";
$func["player_get_hand_played"]["round"] = "";

$func["player_get_played_card"]["game_id"] = "";
$func["player_get_played_card"]["user_id"] = "";
$func["player_get_played_card"]["round"] = "";
$func["player_get_played_card"]["turn"] = "";

$func["player_get_played_card_auto"]["game_id"] = "";
$func["player_get_played_card_auto"]["user_id"] = "";

$func["player_get_waterlevel"]["game_id"] = "";
$func["player_get_waterlevel"]["user_id"] = "";
$func["player_get_waterlevel"]["round"] = "";

$func["player_get_current_points"]["game_id"] = "";
$func["player_get_current_points"]["user_id"] = "";





$select_func = "";
if(isset($_GET['select_func'])){
	$select_func = $_GET['select_func'];
}



/****** CALL FUNCTION ******/

echo "<table border=1 width=100% height=100%><tr>";
echo "<td valign=top>";
if(isset($_POST['select_func'])){
	echo "<pre style='white-space: pre-wrap;'>";
	//echo $_POST['param_count'];
	
	for($i=0;$i<$_POST['param_count'];$i++){
		if($_POST[$i] == "BOOL_TRUE"){
			$para_arr[$i] = TRUE;
		}else if($_POST[$i] == "BOOL_FALSE"){
			$para_arr[$i] = FALSE;
		}else{
			$para_arr[$i] = $_POST[$i];
		}
		
	}
	echo "<u>Arguments:</u>\n";
	var_dump($para_arr);
	
	echo "<hr>";
	
	if($para_arr){
		$func_result = (call_user_func_array($_POST["select_func"], $para_arr));
	}else{
		$func_result = ($_POST["select_func"]());
	}
	echo "\n<u>Return value:</u>\n";
	echo var_dump($func_result);
	echo "</pre>";
	
}
echo "</td>";


echo "<td width=400 valign=top>";
echo "<a href='index.php'>Reset</a>&nbsp;&nbsp;&nbsp;";
echo "<a href='index.php?select_func=$select_func'>Reload</a>&nbsp;&nbsp;&nbsp;";
echo "<a target=_blank href='index.php'>New</a><br>";

echo "<form name=func_form action=\"index.php\" method=get>";
echo "<select name=select_func onchange='document.forms[\"func_form\"].submit()'>";
foreach ($func as $name => $para){
	$sel = "";
	if($select_func == $name){
		$sel = "selected";
	}
	echo "<option $sel value='$name'>".$name."</option>";
}
echo "</select>";
echo "<input type=submit value=' Go '>";

echo "</form>";


if($select_func != ""){
	echo "$select_func():<br>";
	$param_count = 0;
	echo "<form action=\"index.php?select_func=$select_func\" method=post>";
	echo "<input type=hidden value=$select_func name=select_func>";
	echo "<table>";
	if(is_array($func[$select_func]))
	foreach($func[$select_func] as $param_name => $param){
		echo "<tr>";
		echo "<td>";
		echo $param_name;
		echo "</td>";
		
		echo "<td>";
		if(is_string($param)){
			$val = $param;
			if(isset($_POST[$param_count])){
				$val = $_POST[$param_count];
			}
			echo "<input name=$param_count type=text value='$val'>";
			
		}
		
		if(is_array($param)){
			echo "<select name=$param_count>";
			foreach($param as $opt){
				$sel = "";
				if($_POST[$param_count] == $opt){
					$sel = "selected";
				}
				echo "<option $sel value='$opt'>$opt</option>";
			}
			echo "</select>";
			
		}
		echo "</td>";
		echo "</tr>";
		$param_count++;
	}
	echo "<tr><td></td><td><input type=submit value=' Execute '></td></tr>";
	echo "</table>";
	
	
	echo "<input type=hidden value=$param_count name=param_count>";
	echo "</form>";
}

echo "</td>";
echo "</tr></table>";





